package drds.global_transaction.common;

/**
 * 根据IP和端口 再加上流水号 构建唯一的事务ID
 */
public class GlobalTransactionIds
{

	private static String ipAddress;
	private static int port;
	//

	//
	public static String generateGlobalTransactionIdWithAddressAndPort(long globalTransactionId)
	{
		return ipAddress + ":" + port + ":" + globalTransactionId;
	}

	//
	public static String getAddressAndPort(String globalTransactionIdWithAddressAndPort)
	{
		if (globalTransactionIdWithAddressAndPort == null)
		{
			return null;
		}

		int lastIndex = globalTransactionIdWithAddressAndPort.lastIndexOf(":");
		return globalTransactionIdWithAddressAndPort.substring(0, lastIndex);
	}

	public static long getGlobalTransactionId(String globalTransactionIdWithAddressAndPort)
	{
		if (globalTransactionIdWithAddressAndPort == null)
		{
			return -1;
		}
		int lastIndex = globalTransactionIdWithAddressAndPort.lastIndexOf(":");
		return Long.parseLong(globalTransactionIdWithAddressAndPort.substring(lastIndex + 1));
	}

	public static void setIpAddress(String ipAddress)
	{
		GlobalTransactionIds.ipAddress = ipAddress;
	}

	public static void setPort(int port)
	{
		GlobalTransactionIds.port = port;
	}

}
